﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace TaskDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var actions = new List<Action<int>>
            {
                threadId => Console.WriteLine($"A1-{threadId}"),
                threadId => Console.WriteLine($"A2-{threadId}"),
                threadId => Console.WriteLine($"A3-{threadId}"),
                threadId => Console.WriteLine($"A4-{threadId}"),
                threadId => Console.WriteLine($"A5-{threadId}"),
                threadId => Console.WriteLine($"A6-{threadId}"),
                threadId => Console.WriteLine($"A7-{threadId}"),
                threadId => Console.WriteLine($"A8-{threadId}"),
                threadId => Console.WriteLine($"A9-{threadId}"),
                threadId => Console.WriteLine($"A10-{threadId}")
            };
            //foreach (var action in actions)
            //{
            //    var a = action;
            //    ThreadPool.QueueUserWorkItem(state => a(), null);
            //}

            Parallel.ForEach(actions, new ParallelOptions { MaxDegreeOfParallelism = 3 }, (action) =>
            {
                action(Environment.CurrentManagedThreadId);
            });

            // Task async任务测试
            Execute().ContinueWith(t =>
            {
                Console.WriteLine($"task complete result: {t.Result}");
            });

            Console.ReadKey();
        }

        static Task<int> Execute()
        {
            var tsc = new TaskCompletionSource<int>();

            Task.Delay(3000).ContinueWith(t =>
            {
                tsc.SetResult(1);
            });

            return tsc.Task;
        }
    }
}
